<?php

namespace app\index\service;

use app\common\model\CustomPush;
use app\index\dao\CustomPushDao;
use app\index\model\UM as UM;
use ShidianUtil\Utils\ArrayUtil;
use ShidianUtil\Utils\ConvertUtil;
use ShidianUtil\Utils\StringUtil;
use think\exception\ValidateException;

/**
 * Class CustomPushService
 * @package app\index\service
 */
class CustomPushService
{
    // dao
    private CustomPushDao $customPushDao;

    public function __construct(CustomPushDao $customPushDao)
    {
        $this->customPushDao = $customPushDao;
    }

    /**
     * 查询
     * @param UM|null $user
     * @return CustomPush
     */
    public function read(UM $user): CustomPush
    {
        $memberId = $user->id;

        //这里如果要修改查询，建议把查询放到dao层去
        $record = $this->customPushDao->findByMemberId($memberId);
        if (is_null($record)) {
            $default = [
                'memberId' => $memberId,
                'pushStatus' => 0,
                'logic' => 'AND',
                'cityArr' => [],
                'sourceTypeArr' => [],
                'kwsPushStatus' => 0,
                'kwsStr' => '',
                'kws' => [],
            ];
            $record = new CustomPush($default);
        }
        return $record;
    }

    /**
     * 新增
     * @param UM $user
     * @param array $params
     * @return void
     */
    public function add(UM $user, array $params): void
    {
        $params['memberId'] = $user->id;

        $params['pushStatus'] ??= '1';
        $params['pushStatus'] = $params['pushStatus'] == '1' ? '1' : '0';

        $params['cityArr'] = $params['cityArr'] ?? [];
        $params['sourceTypeArr'] = $params['sourceTypeArr'] ?? [];

        if (!is_array($params['cityArr'])) {
            throw new ValidateException('参数[cityArr]错误');
        }

        if (!is_array($params['sourceTypeArr'])) {
            throw new ValidateException('参数[sourceTypeArr]错误');
        }

        $params['kwsPushStatus'] ??= '1';
        $params['kwsPushStatus'] = $params['kwsPushStatus'] == '1' ? '1' : '0';
        $params['kwsStr'] ??= '';

        $this->replaceCustomPush($params['memberId'], $params['pushStatus'], $params['cityArr'], $params['sourceTypeArr'], $params['kwsPushStatus'], $params['kwsStr']);
    }

    /**
     * 插入定制推送
     * @param $memberId
     * @param $pushStatus
     * @param $cityArr
     * @param $sourceTypeArr
     * @param $kwsPushStatus
     * @param $kwsStr
     * @return void
     */
    public function replaceCustomPush($memberId, $pushStatus, $cityArr, $sourceTypeArr, $kwsPushStatus, $kwsStr): void
    {
        $params = [
            'memberId' => $memberId,
            'pushStatus' => $pushStatus,
            'cityArr' => $cityArr,
            'sourceTypeArr' => $sourceTypeArr,
            'kwsPushStatus' => $kwsPushStatus,
            'kwsStr' => $kwsStr,
        ];

        $params['city'] = array_column($params['cityArr'], 'areas_num');
        $params['sourceType'] = array_column($params['sourceTypeArr'], 'name');

        $params['kws'] = ConvertUtil::explode(StringUtil::replace($kwsStr, [',', '，', ' ', '、', '-'], ','), ',');
        $params['kws'] = ArrayUtil::filter($params['kws'], fn($kw) => not_blank($kw));

        $this->customPushDao->replaceActive($params, []);
    }

}
